package com.googlecode.afdbaccess.commandlinedemo.db;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.googlecode.afdbaccess.cache.Transaction;
import com.googlecode.afdbaccess.cache.exceptions.DataLayerException;
import com.googlecode.afdbaccess.cache.resultconversion.helpers.ManyRowsResultSetConverter;


public class NormalQuery {
    public static class NormalQueryResult {
        public final long id;
        public final String name;
        
        private NormalQueryResult(long id, String name) {
            this.id = id;
            this.name = name;
        }
    }

    private static class Private {
        public static class Converter extends ManyRowsResultSetConverter<NormalQueryResult> {
            @Override
            public NormalQueryResult convertSingleRowToData(ResultSet rs) throws SQLException {
                long id = rs.getLong("id");
                String name = rs.getString("name");

                return new NormalQueryResult(id, name);
            }
        }
    }

    public static List<NormalQueryResult> doNormalQuery(Transaction t) throws DataLayerException {
        String sql = "";
        sql += "(\n";
        sql += "SELECT\n";
        sql += "  1 id,\n";
        sql += "  'test row 1' name\n";
        sql += ") UNION ALL (\n";
        sql += "SELECT\n";
        sql += "  2 id,\n";
        sql += "  'test row 2' name\n";
        sql += ")\n";
//        sql += "FROM\n";
//        sql += "  aTable\n";
//        sql += "WHERE\n";
//        sql += "  something < somethingelse\n";

        return t.executeQuery(sql, null, Private.Converter.class, 60*1000); // The last parameter will allow up to 60000 milliseconds old data to be returned from the cache
    }
}
